package com.b2b.controller.system;

import com.b2b.exception.CustomException;
import com.b2b.exception.CustomUnauthorizedException;
import com.b2b.model.SystemRoleDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.service.SystemRoleService;
import com.b2b.util.SystemRoleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemRole")
@PropertySource("classpath:config.properties")
public class SystemRoleController {

    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final SystemRoleService systemRoleService;
    private final SystemRoleUtil systemRoleUtil;

    @Autowired
    public SystemRoleController(SystemRoleService systemRoleService, SystemRoleUtil systemRoleUtil){
        this.systemRoleService = systemRoleService;
        this.systemRoleUtil = systemRoleUtil;
    }

    @PostMapping("/add")
    public ResponseBean add(@RequestBody SystemRoleDto systemRoleDto){
        int count = systemRoleUtil.insertSystemRole(systemRoleDto);
        if (count == -2){
            throw new CustomException("名称已存在！新增失败(Insert Failure)");
        }else if (count <= 0){
            throw new CustomException("新增失败(Insert Failure)");
        }
        systemRoleDto.setRoleId(count);
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", systemRoleDto);
    }

    @GetMapping("/byId")
    public ResponseBean get(@RequestParam Integer roleId){
        SystemRoleDto systemRoleDtoTemp = systemRoleUtil.getById(roleId);
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Select Success)", systemRoleDtoTemp);
    }

    @GetMapping("/getAll")
    public ResponseBean getAll(){
//        if (baseDto.getPage() == null || baseDto.getRows() == null) {
//            baseDto.setPage(1);
//            baseDto.setRows(10);
//        }
//        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
        List<SystemRoleDto> systemRoleDtos = systemRoleUtil.getAll();
//        PageInfo<SystemRoleDto> selectPage = new PageInfo<SystemRoleDto>(systemRoleDtos);
//        if (systemRoleDtos == null || systemRoleDtos.size() < 0) {
//            throw new CustomException("查询失败(Query Failure)");
//        }
//        Map<String, Object> result = new HashMap<String, Object>(16);
//        result.put("count", selectPage.getTotal());
//        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", systemRoleDtos);
    }
    @PutMapping("/adminRole")
    public ResponseBean updateRole(@RequestParam Integer adminId,@RequestParam Integer roleId){
        Integer count = systemRoleUtil.updateSystemAdmin(adminId,roleId);
        if (count <= 0){
            throw new CustomException("更新失败(Select Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Select successful)", null);
    }
    @PutMapping("/update")
    public ResponseBean update(SystemRoleDto systemRoleDto){
        int count = systemRoleUtil.updateSystemRole(systemRoleDto);
        if (count == 2){
            throw new CustomUnauthorizedException("该名称不存在(Account not exist.)");
        }
        if (count <= 0) {
            throw new CustomException("更新失败(Update Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Update Success)", systemRoleDto);
    }
    @DeleteMapping("/delete")
    // @RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean delete(@RequestBody SystemRoleDto systemRoleDto) {
        int count = systemRoleUtil.deleteSystem(systemRoleDto);
        if (count == 2){
            throw new CustomException("删除失败，ID不存在(Deletion Failed. ID does not exist.)");
        }
        if (count <= 0) {
            throw new CustomException("删除失败");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }
}
